package com.ygy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ygy.entity.Favorites;
import com.ygy.entity.Products;
import com.ygy.mapper.FavoritesMapper;
import com.ygy.service.IFavoritesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 收藏表 服务实现类
 * </p>
 *
 * @author Ye
 * @since 2024-03-14
 */
@Service
public class FavoritesServiceImpl extends ServiceImpl<FavoritesMapper, Favorites> implements IFavoritesService {

    @Autowired
    private FavoritesMapper favoritesMapper;

    /*public Page<Products> getFavoriteProductsByUserId(int userId, int pageNum, int pageSize) {
        Page<Products> page = new Page<>(pageNum, pageSize);
        long offset = (pageNum - 1) *pageSize; // 计算偏移量

        // 执行分页查询
        List<Products> productList = favoritesMapper.selectFavoriteProductsByUserId(userId, offset, pageSize);

        // 将查询结果设置到分页对象中
        page.setRecords(productList);
        page.setTotal(productList.size()); // 注意：这里需要实际计算总数，这里只是示例，实际开发中应该使用COUNT查询

        return page;
    }*/

    public Page<Products> getFavoriteProductsByUserId(int userId, int pageNum, int pageSize) {
        Page<Products> page = new Page<>(pageNum, pageSize);
        long offset = (pageNum - 1) * pageSize;

        // 执行分页查询获取产品列表
        List<Products> productList = favoritesMapper.selectFavoriteProductsByUserId(userId, offset, pageSize);
        page.setRecords(productList);

        // 执行查询获取总记录数
        int total = favoritesMapper.selectFavoriteProductsCountByUserId(userId);
        page.setTotal(total);

        return page;
    }



}
